package cn.com.bluemoon.daps.system.entity;

import cn.com.bluemoon.daps.common.domain.BaseModel;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.common.toolkit.AesEncryptUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;


/**
 * <p>
 * 沙箱环境基本配置
 * 不维护数据库名+schema等
 *
 * </p>
 *
 * @author admin
 * @since 2021-06-25
 */
@Data
@Accessors
@ApiModel(value = "DapSystemConfigSandbox", description = "沙箱环境基本配置")
public class DapSystemConfigSandbox extends BaseModel<DapSystemConfigSandbox> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "服务ip")
    private String host;

    @ApiModelProperty(value = "服务端口")
    private Integer port;

    @ApiModelProperty(value = "登录用户")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "库名，目前uuid32生成")
    @TableField(exist = false)
    private String dbName;

    @ApiModelProperty(value = "是否ssl登录")
    private Integer sslConnect;

    @ApiModelProperty(value = "ssl证书")
    private String sslCerti;

    public String getUrl(DatabaseType dbType, String dbname) {
        String url = String.format("jdbc:%s://%s:%d/%s", dbType.getDbName(), this.host, this.port, dbname);
        return url;
    }

    public String decryptPassword() {
        if (password == null) {
            return null;
        }
        try {
            return AesEncryptUtils.decrypt(this.password);
        } catch (Exception e) {
            return this.password;
        }
    }

    /**
     * 生产一个唯一库名
     */
    public void generateDbname() {
        String uuid = IdWorker.get32UUID();
        this.dbName = uuid;
    }
}
